package com.service.impl;

import com.entity.GoodsSort;
import com.mapper.GoodsSortMapper;
import com.service.GoodsSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xuan
 * @Date: 2024/7/3 16:54
 * @Version: 1.0
 */
@Service
public class GoodsSortServiceImpl implements GoodsSortService {
    @Autowired
     private GoodsSortMapper goodsSortMapper;

    @Override
    public List<GoodsSort> findTree() {
       List<GoodsSort> goodsSorts =  goodsSortMapper.findAll();
       List<GoodsSort> goodsSortList = new ArrayList<>();
        for (GoodsSort goodsSort : goodsSorts) {
            if(goodsSort.getGsgsid() == 0){
                //加入新集合并且给children赋值
                goodsSort.setChildren(new ArrayList<>());
                goodsSortList.add(goodsSort);
                //再次遍历添加信息
                for (GoodsSort sort : goodsSorts) {
                    //子gsgsid,等于父gsid
                    if(sort.getGsgsid().equals(goodsSort.getGsid())){
                        goodsSort.getChildren().add(sort);
                    }
                }
            }

        }
        return goodsSortList;
    }
}
